package com.xyzwps.ewa.modules.me

import com.xyzwps.ewa.common.ServerException
import com.xyzwps.ewa.data.database
import com.xyzwps.ewa.ext.userId
import com.xyzwps.ewa.modules.user.UserService

import com.xyzwps.ewa.modules.auth.auth
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.jetbrains.exposed.sql.transactions.transaction

fun Route.meRoutes() {
    auth {
        get {
            val userId = call.attributes.userId()
            val user = transaction {
                UserService.read(userId)
            } ?: throw ServerException("Unauthenticated", HttpStatusCode.Unauthorized)
            call.respond(HttpStatusCode.OK, user)
        }
    }
}